package cn.edu.scau.cmi.wujiaxian.exercise11;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;

public class LP extends JLayeredPane implements MouseListener, MouseMotionListener{

	private static final long serialVersionUID = -3679034342920872466L;
	JButton button;
	int x;
	int y;
	int a;
	int b;
	int x0;
	int y0;
	
	public LP() {
		button = new JButton("用鼠标拖动我");
		button.addMouseListener(this);
		button.addMouseMotionListener(this);
		setLayout(new FlowLayout());
		add(button, JLayeredPane.DEFAULT_LAYER);
	}
	
	@Override
	public void mouseDragged(MouseEvent e) {
		Component component = null;
		if (e.getSource() instanceof Component) {
			component = (Component) e.getSource();
			a = component.getBounds().x;
			b = component.getBounds().y;
			x = e.getX();
			y = e.getY();
			a = a + x;
			b = b + y;
			component.setLocation(a - x0, y - x0);
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		JComponent component = null;
		component = (JComponent) e.getSource();
		setLayer(component, JLayeredPane.DRAG_LAYER);
		a = component.getBounds().x;
		b = component.getBounds().y;
		x0 = e.getX();
		y0 = e.getY();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		JComponent component = null;
		component = (JComponent) e.getSource();
		setLayer(component, JLayeredPane.DRAG_LAYER);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}
	
	
	
}
